<?php

class resource extends Controller {
 	function __construct()
 	{
 		parent::Controller();
		$this->load->model('Resource_Model');
		$this->load->model('Req_data_Model');
 		$this->load->helper('url');
 	}
 	function index()
 	{
 		
 		$data['resource_page'] = $this->Resource_Model->query_resource(5,1)->result();

		$data['Req_data_page'] = $this->Req_data_Model->query_Req_data(5,1)->result();

 		$this->load->view('resource',$data);
 	}


	function new_resource($senderID,$type,$title,$content,$time,$downloadNumber,$URL){
		$this->Resource_Model->insert_resource($senderID,$type,$title,$content,$time,$downloadNumber,$URL);
	}
	
	function new_Req_data($ID,$User,$Content){
		$this->Req_data_Model->insert_Req_data($ID,$User,$Content);
	}
	
	function delete_resource($ID){
		$this->Resource_Model->delete_resource($ID);
	}
	
	function update_resource($ID,$senderID,$type,$title,$content,$time,$downloadNumber,$URL){
		$this->Resource_Model->update_resource($ID,$senderID,$type,$title,$content,$time,$downloadNumber,$URL);
	}
	
	
	function get_resource($ID){
		return $this->Resource_Model->query_resource_by_ID($ID)->result();
	}
	
	function get_Req_data($ID){
		return $this->Req_data_Model->query_Req_data_by_ID($ID)->result();
	}

	
	function get_resource_total_rows(){
		return count($this->Resource_Model->getAll()->result());
	}

	function get_req_data_total_rows(){
		return count($this->Req_data_Model->getAll()->result());
	}


	function get_resource_page($page_number){
		$result = $this->Resource_Model->query_resource($this->per_page, $page_number)->result();
		$ret["totlePage"] = ($this->get_resource_total_rows() / $this->per_page);
		if(($this->get_resource_total_rows() % $this->per_page))
			$ret["totlePage"]++;
		$ret["nowPage"] = $page_number;
		$cnt = 0;
		foreach ($result as $row){
			$ret["title"][$cnt] = $row->title;
			$ret["type"][$cnt] = $row->type;
			$ret["date"][$cnt] = $row->time;
			$ret["date"][$cnt] = $row->time;

			$cnt++;
		}
		$temp1 = ""; $temp2 = "";
		for($i = 0; $i < $cnt-1; $i++){
			$temp1 = sprintf("\"title\":\"%s\",\"type\":\"%s\",\"date\":\"%s\",\"date\":\"%s\"", $ret["title"][$i],$ret["type"][$i], $ret["date"][$i] ,$ret["date"][$i]);
			$temp2 = $temp2 . $temp1 . ",";
		}

		$temp1 = sprintf("\"title\":\"%s\",\"type\":\"%s\",\"date\":\"%s\",\"date\":\"%s\"", $ret["title"][$cnt-1],$ret["type"][$cnt-1], $ret["date"][$cnt-1] ,$ret["date"][$cnt-1]);
		$temp2 = $temp2 . $temp1;
		
		$ret_string = sprintf("{\"totlePage\":\"%d\",\"nowPage\":\"%d\",\"content\":[%s]}", $ret["totlePage"], $ret["nowPage"], $temp2);
		echo $ret_string;
	}


	function get_req_data_page($page_number){
		$result = $this->Req_data_Model->query_req_data($this->per_page, $page_number)->result();
		$ret["totlePage"] = ($this->get_req_data_total_rows() / $this->per_page);
		if(($this->get_req_data_total_rows() % $this->per_page))
			$ret["totlePage"]++;
		$ret["nowPage"] = $page_number;
		$cnt = 0;
		foreach ($result as $row){
			$ret["content"][$cnt] = $row->Content;
			$cnt++;
		}
		
		$temp1 = ""; $temp2 = "";
		for($i = 0; $i < $cnt-1; $i++){
			$temp1 = sprintf("\"content\":\"%s\"", $ret["content"][$i]);
			$temp2 = $temp2 . $temp1 . ",";
		}
		$temp1 = sprintf("\"content\":\"%s\"", $ret["content"][$cnt-1]);
		$temp2 = $temp2 . $temp1;
		
		$ret_string = sprintf("{\"totlePage\":\"%d\",\"nowPage\":\"%d\",\"content\":[%s]}", $ret["totlePage"], $ret["nowPage"], $temp2);
		echo $ret_string;
	}
	